package com.it.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {


    public static final String myOneExchange = "myOneExchange";
    public static final String myOneQueuey = "myOneQueuey";
    public static final String myOneRouting = "info.one";


    @Bean(myOneExchange)
    public Exchange createOneExchange() {
        return ExchangeBuilder.topicExchange(myOneExchange).durable(true).build();
    }

    @Bean(myOneQueuey)
    public Queue createOneQueue() {
        Map<String, Object> params = new HashMap<>();
        // x-dead-letter-exchange 声明了队列里的死信转发到的DLX名称，
        params.put("x-dead-letter-exchange", myTwoExchange);
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的 routing-key 名称。
        params.put("x-dead-letter-routing-key", myTwoRouting);

        return new Queue(myOneQueuey, true, false, false, params);
    }

    @Bean
    public Binding bindingOneQueueToExchange(@Qualifier(myOneExchange) Exchange exchange, @Qualifier(myOneQueuey) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(myOneRouting).noargs();
    }







    public static final String myTwoExchange = "myTwoExchange";
    public static final String myTwoQueuey = "myTwoQueuey";
    public static final String myTwoRouting = "info.two";


    @Bean(myTwoExchange)
    public Exchange createTwoExchange() {
        return ExchangeBuilder.topicExchange(myTwoExchange).durable(true).build();
    }

    @Bean(myTwoQueuey)
    public Queue createTwoQueue() {
        return new Queue(myTwoQueuey, true, false, false, null);
    }

    @Bean
    public Binding bindingTwoQueueToExchange(@Qualifier(myTwoExchange) Exchange exchange, @Qualifier(myTwoQueuey) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(myTwoRouting).noargs();
    }


}
